home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14272 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  143 lines

  1. Path: news.logicon.com!newsmaster@klee
  2. From: kkolda@logicon.com (Kenneth D. Kolda)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Is WATCOM 10.5 a broken compiler or am I stupid?
  5. Date: 29 Mar 1996 18:29:39 GMT
  6. Organization: Logicon Operating Systems
  7. Message-ID: <4jha6j$pf4@piper.logicon.com>
  8. References: <315A8A29.613B4EBA@rz.tu-ilmenau.de>
  9. NNTP-Posting-Host: 137.51.122.161
  10. Mime-Version: 1.0
  11.  
  12. In article <315A8A29.613B4EBA@rz.tu-ilmenau.de>, ai108@rz.tu-ilmenau.de 
  13. says...
  14. >
  15. >Hi.
  16. >
  17. >Yesterday I came across a strange error caused by following code:
  18. >
  19. >typedef struct cstringTEMP
  20. >{
  21. > char len;
  22. > char data[255];
  23. > public:
  24. > cstringTEMP & operator=(char *);
  25. >} cstring;
  26. >
  27. >char pchar2cstring(cstring &s, char *c)
  28. >// Returns 1 for "c to long". 
  29. >// Returns 0 for success.
  30. >{
  31. > unsigned l=strlen(c);
  32. > if( l>255)
  33. >  return 1;
  34. > s.len=(char )l;
  35. > memcpy(s.data,c,l);
  36. > return 0;
  37. >}
  38. >
  39. >cstring & cstring::operator=(char *s)
  40. >{
  41. > if( pchar2cstring(*this,s))
  42. >  len=0;
  43. > return *this;
  44. >}
  45. >
  46. >
  47. >
  48. >
  49. >AND THIS IS THE CODE I TRIED TO USE:
  50. >
  51. > cstring cs="test";
  52. >
  53. >
  54. >IT GAVE THIS ERROR-MESSAGE: (I CUT OUT THE LINE NUMBERS AND THE FILENAME
  55. >TO SAVE SPACE.)
  56. >Error! E400: (col 13) cannot convert right expression for initialization 
  57. >Note! N643: (col 13) cannot convert argument 1 defined in: 
  58. >Note! N630: (col 13) source conversion type is "char *" 
  59. >Note! N631: (col 13) target conversion type is "cstringTEMP const &" 
  60. >
  61. >CHANGING THE LINE ABOVE TO:
  62. >
  63. >cstring cs;
  64. >cs="test";
  65. >
  66. >
  67. >AND THE ERROR WAS NOT LONGER THERE.
  68. >
  69. >And now are my questions
  70. >1.) Is this all my fault or a bug in the WATCOM C++ compiler.
  71. >2.) If it is my fault how do I have to change the declaration.
  72. >
  73. >
  74. >Thanks in advance
  75. >
  76. >Lars
  77.  
  78. No, you're compiler's not crazy.  When you declare and initialize a 
  79. variable in a single statement, C++ assumes you want to use a contructor 
  80. for that initialization.  So, if you redeclare cstring as:
  81.  
  82. struct cstring {
  83.     char len;
  84.     char data[255];
  85.     cstring& operator=(const char*);
  86.     cstring(const char* s) { *this = s; };  // A cstring constructor
  87. }
  88.  
  89.  
  90. then your initialization will work.
  91.  
  92. Ken Kolda
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.